package Form;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import Control.CommonControls;
import Control.Customer_Controller;
import Entity.User;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class Customer_LogIn extends JPanel{
	private JLabel lblTitle;
	private JPanel logInPanel;
	private JLabel lblUsername;
	private JTextField tb_username;
	private JLabel lblPassword;
	private JPasswordField PB_Password;
	private JButton btnLogIn;
	private JLabel lbl_ForgetPwd;
	private JLabel lblError;

	User u = new User();
	JDialog ForgetPwd;
	private JFrame parser;
	private Customer_Controller control;
	private JLabel lblSignup;
	private JLabel lblBG;
	private JTextArea TAabboutus;
	
	public JFrame getParser() {
		return parser;
	}

	public void setParser(JFrame parser) {
		this.parser = parser;
	}

	public Customer_Controller getControl() {
		return control;
	}

	public void setControl(Customer_Controller control) {
		this.control = control;
	}

	public Customer_LogIn(JFrame parser) {
		setParser(parser);
		control = new Customer_Controller();
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {30, 130, 350, 130, 30, 0};
		gridBagLayout.rowHeights = new int[] {80, 250, 30};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0};
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 3;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 0;
		add(getLblTitle(), gbc_lblTitle);
		GridBagConstraints gbc_logInPanel = new GridBagConstraints();
		gbc_logInPanel.insets = new Insets(0, 0, 5, 5);
		gbc_logInPanel.fill = GridBagConstraints.BOTH;
		gbc_logInPanel.gridx = 2;
		gbc_logInPanel.gridy = 1;
		add(getLogInPanel(), gbc_logInPanel);
		GridBagConstraints gbc_lblBG = new GridBagConstraints();
		gbc_lblBG.gridheight = 3;
		gbc_lblBG.gridwidth = 5;
		gbc_lblBG.gridx = 0;
		gbc_lblBG.gridy = 0;
		add(getLblBG(), gbc_lblBG);
	}

	public JLabel getLblTitle() {
		if (lblTitle == null) {
			lblTitle = new JLabel("BeSTWiZ AMK Needy Resident System");
			lblTitle.setFont(new Font("Tahoma", Font.BOLD, 28));
		}
		return lblTitle;
	}
	public JPanel getLogInPanel() {
		if (logInPanel == null) {
			logInPanel = new JPanel();
			logInPanel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
			logInPanel.setBackground(new Color(0, 0, 0, 200));
			GridBagLayout gbl_logInPanel = new GridBagLayout();
			gbl_logInPanel.columnWidths = new int[]{0, 70, 0, 0, 0};
			gbl_logInPanel.rowHeights = new int[]{0, 45, 0, 0, 33, 0, 0, 0, 0};
			gbl_logInPanel.columnWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
			gbl_logInPanel.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
			logInPanel.setLayout(gbl_logInPanel);
			GridBagConstraints gbc_lblError = new GridBagConstraints();
			gbc_lblError.anchor = GridBagConstraints.WEST;
			gbc_lblError.gridwidth = 2;
			gbc_lblError.insets = new Insets(0, 0, 5, 5);
			gbc_lblError.gridx = 1;
			gbc_lblError.gridy = 1;
			logInPanel.add(getLblError(), gbc_lblError);
			GridBagConstraints gbc_lblUsername = new GridBagConstraints();
			gbc_lblUsername.fill = GridBagConstraints.HORIZONTAL;
			gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
			gbc_lblUsername.gridx = 1;
			gbc_lblUsername.gridy = 2;
			logInPanel.add(getLblUsername(), gbc_lblUsername);
			GridBagConstraints gbc_tb_username = new GridBagConstraints();
			gbc_tb_username.insets = new Insets(0, 0, 5, 5);
			gbc_tb_username.fill = GridBagConstraints.HORIZONTAL;
			gbc_tb_username.gridx = 2;
			gbc_tb_username.gridy = 2;
			logInPanel.add(getTb_username(), gbc_tb_username);
			GridBagConstraints gbc_lblPassword = new GridBagConstraints();
			gbc_lblPassword.fill = GridBagConstraints.HORIZONTAL;
			gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
			gbc_lblPassword.gridx = 1;
			gbc_lblPassword.gridy = 3;
			logInPanel.add(getLblPassword(), gbc_lblPassword);
			GridBagConstraints gbc_PB_Password = new GridBagConstraints();
			gbc_PB_Password.insets = new Insets(0, 0, 5, 5);
			gbc_PB_Password.fill = GridBagConstraints.HORIZONTAL;
			gbc_PB_Password.gridx = 2;
			gbc_PB_Password.gridy = 3;
			logInPanel.add(getPB_Password(), gbc_PB_Password);
			GridBagConstraints gbc_btnLogIn = new GridBagConstraints();
			gbc_btnLogIn.insets = new Insets(0, 0, 5, 5);
			gbc_btnLogIn.gridx = 2;
			gbc_btnLogIn.gridy = 4;
			logInPanel.add(getBtnLogIn(), gbc_btnLogIn);
			GridBagConstraints gbc_lblSignup = new GridBagConstraints();
			gbc_lblSignup.anchor = GridBagConstraints.WEST;
			gbc_lblSignup.gridwidth = 2;
			gbc_lblSignup.insets = new Insets(0, 0, 5, 5);
			gbc_lblSignup.gridx = 1;
			gbc_lblSignup.gridy = 5;
			logInPanel.add(getLblSignup(), gbc_lblSignup);
			GridBagConstraints gbc_lbl_ForgetPwd = new GridBagConstraints();
			gbc_lbl_ForgetPwd.anchor = GridBagConstraints.WEST;
			gbc_lbl_ForgetPwd.gridwidth = 2;
			gbc_lbl_ForgetPwd.insets = new Insets(0, 0, 5, 5);
			gbc_lbl_ForgetPwd.gridx = 1;
			gbc_lbl_ForgetPwd.gridy = 6;
			logInPanel.add(getLbl_ForgetPwd(), gbc_lbl_ForgetPwd);
		}
		return logInPanel;
	}
	public JLabel getLblUsername() {
		if (lblUsername == null) {
			lblUsername = new JLabel("Username:");
			lblUsername.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblUsername.setForeground(Color.WHITE);
			lblUsername.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblUsername;
	}
	public JTextField getTb_username() {
		if (tb_username == null) {
			tb_username = new JTextField();
			tb_username.setColumns(10);
		}
		return tb_username;
	}
	public JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel("Password:");
			lblPassword.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblPassword.setForeground(Color.WHITE);
			lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblPassword;
	}
	public JPasswordField getPB_Password() {
		if (PB_Password == null) {
			PB_Password = new JPasswordField();
		}
		return PB_Password;
	}
	public JButton getBtnLogIn() {
		if (btnLogIn == null) {
			btnLogIn = new JButton("Log In");
			btnLogIn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if ((getTb_username().getText().equals(""))||(getPB_Password().getPassword().equals(""))){
						lblError.setText("Enter your Userid & password");
						lblError.setVisible(true);
					}
					else { 
						u = control.verifyUser(getTb_username().getText(), new String(getPB_Password().getPassword()));
						
						if(u.getName()!=null){
							
							CommonControls.setUser(u);
							parser.dispose();
							Main.main();
						}
						else{
							u = control.checkUser(getTb_username().getText());
							
							 if(u.getName() != null){
								//Is a member
								lblError.setText("Login Error");
								lblError.setVisible(true);
								lbl_ForgetPwd.setVisible(true);
								CommonControls.setUser(u);
							}
						}
					}
				}
			});
		}
		return btnLogIn;
	}
	public JLabel getLbl_ForgetPwd() {
		if (lbl_ForgetPwd == null) {
			lbl_ForgetPwd = new JLabel("<html><font color=blue><u>Forget your Password?</u></font></html>");
			lbl_ForgetPwd.setOpaque(true);
			lbl_ForgetPwd.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					JDialog pwr = new JDialog();
					pwr.getContentPane().add(new Customer_PasswordRecovery(parser));
					pwr.pack();
					pwr.setLocationRelativeTo(parser);
					pwr.setVisible(true);
					pwr.setBounds(0,0,471,650);
				}
			});
			lbl_ForgetPwd.setVisible(false);			
		}
		return lbl_ForgetPwd;
	}
	public JLabel getLblError() {
		if (lblError == null) {
			lblError = new JLabel("Log In");
			lblError.setFont(new Font("Rockwell", Font.BOLD, 25));
			lblError.setHorizontalAlignment(SwingConstants.LEFT);
			lblError.setForeground(Color.WHITE);
		}
		return lblError;
	}
	public JLabel getLblSignup() {
		if (lblSignup == null) {
			lblSignup = new JLabel("<html><font color=blue><u>Not a member yet?</u></font></html>");
			lblSignup.setOpaque(true);
			lblSignup.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					JDialog SU =  new JDialog();
					SU.getContentPane().add(new Customer_CreateCustomerMembership(parser));
					SU.pack();
					SU.setLocationRelativeTo(parser);
					SU.setVisible(true);
					SU.setBounds(0,0,471,650);
				}
			});
		}
		return lblSignup;
	}
	public JLabel getLblBG() {
		if (lblBG == null) {
			lblBG = new JLabel("");
			lblBG.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage("Resources/Images/Homepage/HomeBg.jpg").getScaledInstance(680, 400, Image.SCALE_DEFAULT)));
		}
		return lblBG;
	}
}
